﻿Imports ATS.EncryptDecryptKeys

Class MainWindow

#Region " - Generate Key Controls - "
    Private Sub btnGenerateProductKey_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnGenerateProductKey.Click
        Dim strRegkey As String = GenerateKey(TextBoxPassPhrase.Text, TextBoxCompanyName.Text)
        If strRegkey.Length > 0 Then
            strRegkey = strRegkey.Remove(16, (strRegkey.Length - 16))
            strRegkey = strRegkey.Insert(4, "-")
            strRegkey = strRegkey.Insert(9, "-")
            strRegkey = strRegkey.Insert(14, "-")
        End If
        TextBoxProductKey.Text = strRegkey
    End Sub

    Private Sub btnGenerateLicenseKey_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnGenerateLicenseKey.Click
        Dim licenseKeyText As String = String.Empty
        If TextBoxProductKey.Text.Length > 0 Then
            If CheckBoxNoExpiration.IsChecked Then
                licenseKeyText = TextBoxProductKey.Text
            Else
                licenseKeyText = TextBoxProductKey.Text & "|" & DatePickerExpirationDate.ToString
            End If
            Dim enc As System.Text.Encoding = System.Text.Encoding.ASCII
            Dim myByteArray As Byte() = enc.GetBytes(GenerateKey(TextBoxPassPhrase.Text, licenseKeyText))
            Dim myString As String = enc.GetString(myByteArray)
            TextBoxLicenseKey.Text = Convert.ToBase64String(myByteArray)
        Else
            MessageBox.Show("Generate product key first.")
        End If
    End Sub
#End Region

#Region " - Common Methods - "
    Private Function GenerateKey(ByVal pPassPhrase As String, ByVal pClientName As String) As String
        Return CommonMethods.Encrypt(pPassPhrase, pClientName.ToUpper)
    End Function

    'Private Function Encrypt(ByRef pPassPhrase As String, ByVal pTextToEncrypt As String) As String
    '    If pPassPhrase.Length > 16 Then
    '        'limitation of the encryption mechanism
    '        pPassPhrase = pPassPhrase.Substring(0, 16)
    '    End If

    '    If pTextToEncrypt.Trim.Length = 0 Then
    '        'the Text to encrypt not set!!!
    '        Return String.Empty
    '    End If

    '    Dim skey As New Encryption.Data(pPassPhrase)
    '    Dim sym As New Encryption.Symmetric(Encryption.Symmetric.Provider.Rijndael)
    '    Dim objEncryptedData As Encryption.Data
    '    objEncryptedData = sym.Encrypt(New Encryption.Data(pTextToEncrypt), skey)
    '    Return objEncryptedData.ToHex
    'End Function
#End Region

    
End Class
